package com.ymatou.riskcontrol.api.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.riskcontrol.api.parameter.OrderItem;
import com.ymatou.riskcontrol.api.parameter.RiskEsblishBean;
import com.ymatou.riskcontrol.api.service.RiskEsblishCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.RiskCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 风控下单接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_RiskEsblish {
	private static RiskEsblishBean reb;
	private static RiskEsblishCall rec;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("风控下单接口");
	}

	@Before
	public void caseUp() {
		reb = new RiskEsblishBean();
		rec = new RiskEsblishCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(rec.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RiskEsblish_001() {
		Logger.start(true,"风控下单校验成功");
		try {
			// Map m = new OrderCallService().appSaveOrder();
			// Logger.debug("AppSaveOrder map:"+m);
			Map m = new HashMap();
			m.put("OrderId", TestDataManager.getClientpaymentId().substring(6, 14));
			m.put("UserId", TestDataManager.getClientpaymentId().substring(8, 14));
			m.put("ActivityId", TestDataManager.getClientpaymentId().substring(10, 14));
			m.put("SellerId", TestDataManager.getClientpaymentId().substring(10, 14));
			m.put("ThirdCategoryId", TestDataManager.getClientpaymentId().substring(6, 14));
			m.put("CatalogId", TestDataManager.getRandomUUID().toString());
			m.put("ProductId", TestDataManager.getRandomUUID().toString());
			m.put("TotalPrice", 10);
			int orderid = Integer.valueOf(m.get("OrderId").toString());
			int userid = Integer.valueOf(m.get("UserId").toString());
			int activityId = Integer.valueOf(m.get("ActivityId").toString());
			int SellerId = Integer.valueOf(m.get("SellerId").toString());
			int ThirdCategoryId = Integer.valueOf(m.get("ThirdCategoryId").toString());
			BigDecimal TotalPrice = new BigDecimal(m.get("TotalPrice").toString());
			String TraceId = TestDataManager.getRandomUUID().toString();
			// 一个订单，单价等于总价
			BigDecimal OrderPrice = TotalPrice;
			String catalogId = m.get("CatalogId").toString();
			// set orderitem
			ArrayList<OrderItem> loi = new ArrayList<OrderItem>();
			OrderItem oi = new OrderItem();
			String productId = m.get("ProductId").toString();
			oi.setProductId(productId);
			oi.setActivityId(activityId);
			oi.setProductNum(1);
			oi.setCatalogId(catalogId);
			oi.setProductPrice(TotalPrice);
			loi.add(oi);
			reb.setOrderItems(loi);
			// other
			reb.setUserId(userid);
			reb.setOrderId(orderid);
			String CouponBatchId = TestDataManager.getRandomUUID().toString();
			reb.setCouponBatchId(CouponBatchId);
			String CouponId = TestDataManager.getRandomUUID().toString();
			reb.setCouponId(CouponId);
			String DeviceId = TestDataManager.getRandomUUID().toString();
			reb.setDeviceId(DeviceId);
			BigDecimal GiftAmount = new BigDecimal(1);
			reb.setGiftAmount(GiftAmount);
			reb.setIp("127.0.0.1");
			String Mobile = TestDataManager.getPhoneNum();
			reb.setMobile(Mobile);
			reb.setOrderPrice(OrderPrice);
			String PayId = TestDataManager.getRandomUUID().toString();
			reb.setPayerId(PayId);
			reb.setPayType("PayType");
			String RecipientMobile = TestDataManager.getPhoneNum();
			reb.setRecipientMobile(RecipientMobile);
			reb.setSellerId(SellerId);
			reb.setSellerName("自动化测试SellerName");
			reb.setUserName("UserName");
			reb.setThirdPartTradingId(String.valueOf(ThirdCategoryId));
			reb.setTradingId(TraceId);
			rec.setData(reb);
			rec.callService();
			Logger.comment("RiskEsblishCall send:" + rec.getEntityString());
			Logger.comment("RiskEsblishCall return:" + rec.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, rec.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, rec.getMsg(), "验证Msg");
			
			/**
			 * {"OrderId":105258509,"UserId":1113,"UserName":"","SellerId":
			 * 288484,"SellerName":"自动化测试SellerName",
			 * "Mobile":"13636011435","DeviceId"
			 * :"bfc12d5f-6586-48f8-86e4-69a9c1a5af12",
			 * "PayerId":null,"PayChannel"
			 * :null,"PayType":"PayType","PaymentId":null
			 * ,"Ip":"127.0.0.1","OrderPrice":404,
			 * "CouponBatchId":"a7d6cc64-695d-4d97-a0f9-2fb56ada2311"
			 * ,"CouponId":"e0ca2f86-2c3b-4106-80c1-a8bedcc036e8",
			 * "GiftAmount":1
			 * ,"TradingId":null,"ThirdPartTradingId":"","RecipientMobile"
			 * :"13842586672", "OrderItems":[{"ProductId":
			 * "01141a7e-bce4-474c-be2b-8e95f8c6b0cc"
			 * ,"CatalogId":"3d7a5414-96ce-433a-9bf1-1646bd18db43"
			 * ,"ProductNum":1,"ProductPrice":404,"ActivityId":8412}]},
			 * "OrderStatus":1,"RiskControlStatus":1,"VerifyStatus":1,"AddTime":
			 * "\/Date(1439265573406)\/","Comment":null,"Auditor":null,
			 * "AuditTime":"\/Date(-62135596800000)\/"}
			 */
			JSONObject json = RiskCallService.getRiskDetail(String.valueOf(orderid));
			// 验证OrderId
			Logger.verifyEquals(orderid, json.get("OrderId"), "验证orderid");
			// 验证OrderItem
			JSONObject OrderItem1 = json.getJSONArray("OrderItems").getJSONObject(0);
			Logger.verifyEquals(activityId, OrderItem1.get("ActivityId"), "验证ActivityId");
			Logger.verifyEquals(catalogId, OrderItem1.get("CatalogId"), "验证CatalogId");
			Logger.verifyEquals(true,
					TestDataManager.equalsBigDecimal(TotalPrice, OrderItem1.get("ProductPrice").toString()),
					"验证ProductPrice:" + TotalPrice);
			Logger.verifyEquals(productId, OrderItem1.get("ProductId"), "验证ProductId");
			Logger.verifyEquals(1, OrderItem1.get("ProductNum"), "验证ProductNum");
			// 验证其他
			Logger.verifyEquals(1, rec.getStatus(), "验证Status");
			Logger.verifyEquals(userid, json.get("UserId"), "验证userid");
			Logger.verifyEquals("UserName", json.get("UserName"), "验证UserName");
			Logger.verifyEquals(SellerId, json.get("SellerId"), "验证SellerId");
			Logger.verifyEquals("自动化测试SellerName", json.get("SellerName"), "验证SellerName");
			Logger.verifyEquals(String.valueOf(ThirdCategoryId), json.get("ThirdPartTradingId").toString(),
					"验证ThirdPartTradingId");
			Logger.verifyEquals(TraceId, json.get("TradingId"), "验证TradingId");
			Logger.verifyEquals(Mobile, json.get("Mobile"), "验证Mobile");
			Logger.verifyEquals(DeviceId, json.get("DeviceId"), "验证DeviceId");
			Logger.verifyEquals(PayId, json.get("PayerId"), "验证PayerId");
			Logger.verifyEquals("PayType", json.get("PayType"), "验证PayType");
			Logger.verifyEquals("127.0.0.1", json.get("Ip"), "验证Ip");
			Logger.verifyEquals(true, TestDataManager.equalsBigDecimal(TotalPrice, json.get("OrderPrice").toString()),
					"验证OrderPrice:" + OrderPrice);
			Logger.verifyEquals(CouponBatchId, json.get("CouponBatchId"), "验证CouponBatchId");
			Logger.verifyEquals(CouponId, json.get("CouponId"), "验证CouponId");
			Logger.verifyEquals(String.valueOf(GiftAmount), json.get("GiftAmount").toString(), "验证GiftAmount");
			Logger.verifyEquals(RecipientMobile, json.get("RecipientMobile"), "验证RecipientMobile");
			// Logger.verifyEquals(1, json.get("OrderStatus"),"验证OrderStatus");
			// Logger.verifyEquals(1,
			// json.get("RiskControlStatus"),"验证RiskControlStatus");
			// Logger.verifyEquals(1,
			// json.get("VerifyStatus"),"验证VerifyStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_RiskEsblish_002() {
		Logger.start(true,"不输入必填项UserId失败");
		try {
			Map m = new OrderCallService().appSaveOrder();
			int orderid = Integer.valueOf(m.get("OrderId").toString());
			// int userid = Integer.valueOf(m.get("UserId").toString());
			int activityId = Integer.valueOf(m.get("ActivityId").toString());
			int SellerId = Integer.valueOf(m.get("SellerId").toString());
			int ThirdCategoryId = Integer.valueOf(m.get("ThirdCategoryId").toString());
			BigDecimal TotalPrice = new BigDecimal(m.get("TotalPrice").toString());
			String TraceId = TestDataManager.getRandomUUID().toString();
			// 一个订单，单价等于总价
			BigDecimal OrderPrice = TotalPrice;
			String catalogId = m.get("CatalogId").toString();
			// set orderitem
			ArrayList<OrderItem> loi = new ArrayList<OrderItem>();
			OrderItem oi = new OrderItem();
			String productId = m.get("ProductId").toString();
			oi.setProductId(productId);
			oi.setActivityId(activityId);
			oi.setProductNum(1);
			oi.setCatalogId(catalogId);
			oi.setProductPrice(TotalPrice);
			loi.add(oi);
			reb.setOrderItems(loi);
			// other
			// reb.setUserId(userid);
			reb.setOrderId(orderid);
			String CouponBatchId = TestDataManager.getRandomUUID().toString();
			reb.setCouponBatchId(CouponBatchId);
			String CouponId = TestDataManager.getRandomUUID().toString();
			reb.setCouponId(CouponId);
			String DeviceId = TestDataManager.getRandomUUID().toString();
			reb.setDeviceId(DeviceId);
			BigDecimal GiftAmount = new BigDecimal(1);
			reb.setGiftAmount(GiftAmount);
			reb.setIp("127.0.0.1");
			String Mobile = TestDataManager.getPhoneNum();
			reb.setMobile(Mobile);
			reb.setOrderPrice(OrderPrice);
			String PayId = TestDataManager.getRandomUUID().toString();
			reb.setPayerId(PayId);
			reb.setPayType("PayType");
			String RecipientMobile = TestDataManager.getPhoneNum();
			reb.setRecipientMobile(RecipientMobile);
			reb.setSellerId(SellerId);
			reb.setSellerName("自动化测试SellerName");
			reb.setUserName("UserName");
			reb.setThirdPartTradingId(String.valueOf(ThirdCategoryId));
			reb.setTradingId(TraceId);
			rec.setData(reb);
			rec.callService();
			Logger.comment("RiskEsblishCall send:" + rec.getEntityString());
			Logger.comment("RiskEsblishCall return:" + rec.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, rec.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, rec.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_RiskEsblish_003() {
		Logger.start(true,"不输入必填项OrderId失败");
		try {
			Map m = new OrderCallService().appSaveOrder();
			// int orderid = Integer.valueOf(m.get("OrderId").toString());
			int userid = Integer.valueOf(m.get("UserId").toString());
			int activityId = Integer.valueOf(m.get("ActivityId").toString());
			int SellerId = Integer.valueOf(m.get("SellerId").toString());
			int ThirdCategoryId = Integer.valueOf(m.get("ThirdCategoryId").toString());
			BigDecimal TotalPrice = new BigDecimal(m.get("TotalPrice").toString());
			String TraceId = TestDataManager.getRandomUUID().toString();
			// 一个订单，单价等于总价
			BigDecimal OrderPrice = TotalPrice;
			String catalogId = m.get("CatalogId").toString();
			// set orderitem
			ArrayList<OrderItem> loi = new ArrayList<OrderItem>();
			OrderItem oi = new OrderItem();
			String productId = m.get("ProductId").toString();
			oi.setProductId(productId);
			oi.setActivityId(activityId);
			oi.setProductNum(1);
			oi.setCatalogId(catalogId);
			oi.setProductPrice(TotalPrice);
			loi.add(oi);
			reb.setOrderItems(loi);
			// other
			reb.setUserId(userid);
			// reb.setOrderId(orderid);
			String CouponBatchId = TestDataManager.getRandomUUID().toString();
			reb.setCouponBatchId(CouponBatchId);
			String CouponId = TestDataManager.getRandomUUID().toString();
			reb.setCouponId(CouponId);
			String DeviceId = TestDataManager.getRandomUUID().toString();
			reb.setDeviceId(DeviceId);
			BigDecimal GiftAmount = new BigDecimal(1);
			reb.setGiftAmount(GiftAmount);
			reb.setIp("127.0.0.1");
			String Mobile = TestDataManager.getPhoneNum();
			reb.setMobile(Mobile);
			reb.setOrderPrice(OrderPrice);
			String PayId = TestDataManager.getRandomUUID().toString();
			reb.setPayerId(PayId);
			reb.setPayType("PayType");
			String RecipientMobile = TestDataManager.getPhoneNum();
			reb.setRecipientMobile(RecipientMobile);
			reb.setSellerId(SellerId);
			reb.setSellerName("自动化测试SellerName");
			reb.setUserName("UserName");
			reb.setThirdPartTradingId(String.valueOf(ThirdCategoryId));
			reb.setTradingId(TraceId);
			rec.setData(reb);
			rec.callService();
			Logger.comment("RiskEsblishCall send:" + rec.getEntityString());
			Logger.comment("RiskEsblishCall return:" + rec.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, rec.getCode(), "验证Code");
			Logger.verifyEquals(ECode.SUCCESSMSG3, rec.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}